package main

import (
	"github.com/kataras/iris/v12"
    "github.com/kataras/iris/v12/mvc"
	"fmt"
	"encoding/json"
	"github.com/go-xorm/xorm"
	_ "github.com/go-sql-driver/mysql"
	"log"
	"time"
)

var x *xorm.Engine

type ProductType struct{
	Id int `xorm:"pk autoincr"`
	Name string
	CreatedAt time.Time
  	UpdatedAt time.Time
}

type Product struct{
	Id int `xorm:"pk autoincr"`
	Name string 
	Type ProductType `xorm:"-"`
	TypeId int
	CreatedAt time.Time
  	UpdatedAt time.Time
}

type ProjectType struct{
	Id int `xorm:"pk autoincr"`
	Name string
	CreatedAt time.Time
  	UpdatedAt time.Time 
}

type Project struct{
	Id int `xorm:"pk autoincr"`
	Name string
	Type ProjectType `xorm:"-"`
	TypeId int
	CreatedAt time.Time
  	UpdatedAt time.Time
}

type Meal struct{
	Id int `xorm:"pk autoincr"`
	Name string 
	ProjectList []*Project `xorm:"-"`
	ProductList []*Product `xorm:"-"`
	CreatedAt time.Time
  	UpdatedAt time.Time
}

type Member struct{
	Id int `xorm:"pk autoincr"`
	Name string 
	Meallist []*Meal `xorm:"-"`
	CreatedAt time.Time
  	UpdatedAt time.Time
}
 
type MealProduct struct{
	Id int `xorm:"pk autoincr"`
	MealId int
	ProductId int
	CreatedAt time.Time
  	UpdatedAt time.Time
}

type MealProject struct{
	Id int `xorm:"pk autoincr"`
	MealId int
	ProjectId int
	CreatedAt time.Time
  	UpdatedAt time.Time
}

type MemberMeal struct{
	Id int `xorm:"pk autoincr"`
	MemberId int
	MealId int
	CreatedAt time.Time
  	UpdatedAt time.Time
}

type JsonController struct{
	Ctx iris.Context
}

func (c *JsonController) BeforeActivation(b mvc.BeforeActivation) {

    b.Handle(
        "POST",
        "/custom",
        "JsonHandler",
    )
}

func (j *JsonController) JsonHandler() interface{} {
	var mb []*Member

	name := j.Ctx.FormValue("name")
    age := j.Ctx.FormValue("age")
	if err := json.Unmarshal([]byte(age), &mb); err == nil{
    //if err := j.Ctx.ReadJSON(&mb); err == nil{
		for _, jv := range mb{
			jv.CreatedAt = time.Now()
			jv.UpdatedAt = time.Now()
			fmt.Println(jv)
			x.Insert(jv)
			for _, mv := range jv.Meallist{
				mv.CreatedAt = time.Now()
				mv.UpdatedAt = time.Now()
				fmt.Println(mv)
				x.Insert(mv)
				memberMeal :=  MemberMeal{
					MemberId : jv.Id,
					MealId : mv.Id,
					CreatedAt : time.Now(),
  					UpdatedAt :	time.Now(),
				}
				x.Insert(&memberMeal)

				for _, mvj := range mv.ProjectList{
					mvj.CreatedAt = time.Now()
					mvj.UpdatedAt = time.Now()
					fmt.Println(mvj)
					mvj.Type.CreatedAt = time.Now()
					mvj.Type.UpdatedAt = time.Now()
					x.Insert(mvj.Type)
					x.Insert(mvj)
					mealProject := MealProject{
						MealId : mv.Id,
						ProjectId : mvj.Id,
						CreatedAt : time.Now(),
  						UpdatedAt :	time.Now(),
					}
					x.Insert(&mealProject)
					
				}
				for _, mvd := range mv.ProductList{
					mvd.CreatedAt = time.Now()
					mvd.UpdatedAt = time.Now()
					fmt.Println(mvd)
					mvd.Type.CreatedAt = time.Now()
					mvd.Type.UpdatedAt = time.Now()
					x.Insert(mvd.Type)
					x.Insert(mvd)
					mealProduct := MealProduct{
						MealId : mv.Id,
						ProductId : mvd.Id,
						CreatedAt : time.Now(),
  						UpdatedAt :	time.Now(),
					}
					x.Insert(&mealProduct)

				}
			}
		}

		return map[string]interface{}{"message": "成功","code":200, "信息":name}

	}else{
		return map[string]interface{}{"message": "error", "code":400}
	}
    
}

func main() {
	var xErr error
	x, xErr = xorm.NewEngine("mysql", "root:new_password@tcp(127.0.0.1:3306)/insert_xorm?charset=utf8")
	if xErr != nil {
		log.Fatal("数据库连接失败:", xErr)
	}
	if err := x.Sync(new(ProductType), new(Product), new(ProjectType), new(Project), new(Meal), new(Member), new(MealProduct), new(MealProject), new(MemberMeal)); err != nil {
		log.Fatal("数据表同步失败:", err)
	}

	app := iris.New()
	
    mvc.Configure(app.Party("/web"), func(context *mvc.Application){
        context.Handle(new(JsonController))
	})
	
    app.Run(iris.Addr(":0007"))
}